<?php

class Common_Model {

    /**
     *
     * @var Zend_Db_Table 
     */
    public $table = null;

    /**
     *
     * @var string
     */
    public $tableName = null;

    /**
     *
     * @var Zend_Db_Adapter_Abstract
     */
    public $db = null;

    const _TPREFIX = 'Application_Model_DbTable_';

    public function __construct() {

        $name = self::_TPREFIX . $this->tableName;
        $this->table = new $name;
        $this->db = Zend_Db_Table::getDefaultAdapter();
    }

    /**
     * 
     * @param type $where
     * @param type $order
     * @param type $count
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function fetchAll($where, $order = null, $count = null) {
        return $this->table->fetchAll($where, $order, $count);
    }

    public function fetchRow($where, $order = null, $count = null) {
        return $this->table->fetchRow($where, $order, $count);
    }

    public function deleteRow($where) {
        return $this->table->delete($where);
    }

    public function getById($id, $where = null) {
        return $this->table->fetchRow('id=' . $id . ($where == null ? '' : ' and ' . $where));
    }

    public function getByEmailAndLogin($email, $login) {
        return $this->table->fetchRow('email like \'' . $email . '\' and login like \'' . $login . '\'');
    }

    public function insert($data) {
        $id = $this->table->insert($data);
        
        return $this->getById($id);
    }

    /**
     * 
     * @return \Zend_Paginator_Adapter_DbSelect
     */
    public function getAdapterPagin() {

        $adapter = new Zend_Paginator_Adapter_DbSelect($this->table->select());

        return $adapter;
    }

    /**
     * 
     * @return \Zend_Form
     */
    public function getForm() {
        $form = new Zend_Form();

        $submit = new Zend_Form_Element_Submit('submit', array(
                    'label' => 'Wyślij',
                    'class' => 'ui-button round-all fr'
                ));
        $back = new Zend_Form_Element_Button('goback', array(
                    'label' => 'Powrót',
                    'class' => 'goback ui-button round-all fl'
                ));

        $back->setDecorators(array('ViewHelper'));
        $submit->setDecorators(array('ViewHelper'));

        $form->addDisplayGroup(array(
            $back, $submit
                ), 'gr-submit', array(
            'order' => 999
        ));

        return $form;
    }

    public function getLoggedUser() {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $ob = $auth->getStorage()->read();
            return $ob->id;
        } else {
            return null;
        }
    }

    /**
     * 
     * @param array $params
     * @return string where casue
     */
    public function buildWhereFromFiltered($params) {
        return null;
    }

    /**
     * 
     * @param array $params
     * @return string order casue
     */
    public function buildOrderFromFiltered($params) {
        return null;
    }

}